package com.midea.iot.course1.javaSE3;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 作业2
 * @author xingwuxw
 * 分析以下需求，并用代码实现：
 * (1)定义一个Worker类型(属性私有、空参有参构造、set、get方法、toString方法、equals方法、hashCode方法)，
 * 包含三个属性：姓名(String name)、年龄(int age)、工资(double salary),在Worker类中定义work方法打印一句话，
 * 如："凤姐 正在工作"，其中"凤姐"代表当前对象的姓名
 * (2)定义List集合，在List集合中增加三个员工，基本信息如下：  "凤姐" 18 20000  "欧阳峰" 60 8000  "刘德华" 40 30000
 * (3)在"欧阳峰"之前插入一个员工，信息为：姓名："张柏芝"，年龄：35，工资3300
 * (4)删除"刘德华"的信息
 * (5)利用for 循环遍历，打印List 中所有员工的信息
 * (6)利用迭代遍历，对List 中所有的员工调用work 方法
 * (7)为Worker类重写equals 方法，当姓名、年龄、工资全部相等时候才返回true
 */
public class Worker {
    private String name;
    private int age;
    private double salary;

    public static void main(String[] args) {
        Worker worker = new Worker();
        worker.work("凤姐");

        List<Worker> list = new ArrayList<>();
        list.add(new Worker("凤姐",18,20000));
        list.add(new Worker("欧阳峰",60,8000));
        list.add(new Worker("刘德华",40,30000));
        list.add(1,new Worker("张柏芝",35,3300));

        list.remove(3);

        for(int i = 0;i<list.size();i++)
        {
            System.out.println(list.get(i));
        }
        System.out.println("迭代器方式");
        Iterator<Worker> it= list.iterator();
        while(it.hasNext())
        {
            Worker p=it.next();
            System.out.println(p.toString());
        }
    }

    public void work (String name){
        System.out.println(name+"正在工作");
    }

    public Worker() {
    }

    public Worker(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public boolean equals(Object object) {
        if (this == object) return true;
        if (object == null || getClass() != object.getClass()) return false;
        if (!super.equals(object)) return false;
        Worker worker = (Worker) object;
        return age == worker.age &&
                Double.compare(worker.salary, salary) == 0 &&
                java.util.Objects.equals(name, worker.name);
    }

    public int hashCode() {
        return java.util.Objects.hash(super.hashCode(), name, age, salary);
    }

    @Override
    public String toString() {
        return  "Worker [姓名：" + name + ", 年龄：" + age + "]"+ ", 工资：" + salary + "]";
    }
}
